#!/bin/bash

function usage() {
    echo "Usage: ${0} [gprof] [profiling] [debug|optimized] [-- rest is passed as is to ./configure]"
    echo " gprof          Build the code with the -g flag for gprof."
    echo " profiling      Build profiling code, define PROFILING."
    echo " debug          Debugging optimization flags for compiler."
    echo " debug-ui       UI debugging support."
    echo " optimized      Optimization flags for compiler."
    echo " symbols        Add degugging symbols."
    echo " mingw32        Build using mingw32."
    echo " mingw64        Build using mingw64."
    echo " w32            Build using mxe.cc."
    echo ' clang-analyzer Build using clang-analyzer, compile using: scan-build --use-analyzer=$(which clang) make'
    exit 1
}

CB='\E[1m'
CB1='\E[1;30m'
CB2='\E[1;34m'
CE='\E[0;00m'


gprof=0
profiling=0
debug=0
debug_ui=0
optimized=0
dsymbols=0
configure="./configure"
win32=0

more=1
while [ ${more} -ne 0 ] ; do
    case "${1}" in
        help)
            usage
            ;;
        debug)
            debug=1
            ;;
        debug-ui)
            debug=1
            debug_ui=1
            ;;
        symbols)
            dsymbols=1
            ;;
        optimized)
            optimized=1
            ;;
        gprof)
            gprof=1
            ;;
        profiling)
            profiling=1
            ;;
        mingw32)
            configure="mingw32-configure --without-lua --datadir=$(pwd)/inst/share --bindir=$(pwd)/inst/bin --mandir=$(pwd)/inst/share/man"
            ;;
        mingw64)
            configure="mingw64-configure --without-lua"
            ;;
        w32)
            win32=1
            configure="./configure --host=i686-pc-mingw32 --disable-libmikmodtest --with-libmikmod-prefix=/usr/local/mxe/usr/i686-pc-mingw32 --with-lib3ds-prefix=/usr/local/mxe/usr/i686-pc-mingw32"
            ;;
        clang-analyzer)
            configure="scan-build --use-analyzer=$(which clang) ./configure"
            ;;
        --)
            more=0
            ;;
        "")
            ;;
        *)
            echo "Unknown argument: \"${1}\""
            usage
            exit 1
            ;;
    esac

    shift
    if [ ${?} -ne 0 ]; then
        more=0
    fi
done

if [ ${debug} -ne 0 -a ${optimized} -ne 0 ]; then
    echo "You specified both 'debug' and 'optimized', only one is allowed."
    exit 1
fi

unset more

unset CFLAGS
unset LDFLAGS

export ACLOCAL_PATH=/usr/local/share/aclocal

# removed -Waggregate-return because json-c fails with it
CWARNFLAGS="-Wextra -Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Wshadow -Wfatal-errors"
CFLAGS=""
LDFLAGS=""
CONFIGURE_OPTIONS=""

if [ ${debug} -ne 0 ]; then
    CFLAGS+=" -O0 -ggdb"
    LDFLAGS+=" -O0 -ggdb"
    if [ ${win32} -eq 0 ]; then
        CFLAGS+=" -rdynamic"
        LDFLAGS+=" -rdynamic"
    fi
else
    CFLAGS+=" -DNDEBUG"
    if [ ${optimized} -ne 0 ]; then
        CFLAGS+=" -O3 -ffast-math -fno-signed-zeros -flto"
        LDFLAGS+=" -O3 -ffast-math -fno-signed-zeros -flto"
#        CONFIGURE_OPTIONS+=" --enable-nonopengl=no"
    fi
    if [ ${dsymbols} -ne 0 ]; then
        CFLAGS+=" -ggdb"
        LDFLAGS+=" -ggdb"
    fi
fi

if [ ${gprof} -ne 0 ]; then
    CFLAGS+=" -pg"
    LDFLAGS+=" -pg"
fi

if [ ${profiling} -ne 0 ]; then
    CFLAGS+=" -DPROFILING"
fi

if [ ${debug_ui} -ne 0 ]; then
    CFLAGS+=" -DUI_DEBUG"
fi

export CFLAGS
export LDFLAGS
export CWARNFLAGS

./autogen.sh
if [ ${?} -ne 0 ]; then
    exit 1
fi

echo -e "${CB}Running configure...${CE}"
echo -e "${CB1}${configure} --prefix=$(pwd)/inst ${CONFIGURE_OPTIONS} ${*}${CE}"
${configure} --prefix=$(pwd)/inst ${CONFIGURE_OPTIONS} ${*}
if [ ${?} -ne 0 ]; then
    exit 1
fi

make clean
if [ ${?} -ne 0 ]; then
    exit 1
fi

if [ -d inst/share/diamond-girl/maps ]; then
    echo ""
    echo -e "${CB}Checking installation directory maps for modifications...${CE}"
    pushd inst/share/diamond-girl/maps > /dev/null
    for map in */* ; do
        if [ -f ${map} ]; then
            cmp ${map} ../../../../maps/${map} > /dev/null
            if [ ${?} -ne 0 ]; then
                echo -e "${CB2} * map ${map} differs, saving changes by copying from inst/.../maps/ to maps/...\e${CE}"
                cp ${map} ../../../../maps/${map} || exit 1
            fi
        fi
    done
    popd > /dev/null
fi

rm -rf inst

echo -e "${CB}"
echo "CWARNFLAGS = ${CWARNFLAGS}"
echo "CFLAGS     = ${CFLAGS}"
echo "LDFLAGS    = ${LDFLAGS}"
echo -e "${CE}"
